﻿using Castle.Core.Internal;
using Ongoal.Quotation.Common;
using Ongoal.Quotation.Service;
using Ongoal.Quotation.Service.Interface;
using Ongoal.Quotation.Service.Model;
using Ongoal.Quotation.Service.Model.Sys;
using Ongoal.Quotation.Service.Param;
using Ongoal.Quotation.Service.ViewModel;
using SqlSugar;
using System.ComponentModel;

namespace Ongoal.Quotation.Service
{
    /// <summary>
    /// 菜单相关实现
    /// </summary>
    internal class MenuService : ServiceBase, IMenuService
    {
        /// <summary>
        /// 获取当前用户对应的菜单
        /// </summary>
        /// <returns></returns>
        public object GetMenu()
        {
            using (var db = Db!.GetDbContext(String.Empty))
            {
                List<Menu> all = new List<Menu>();
                List<OperateModel> operateList;
                if (CurrentUser!.UserType == (int)EUserType.SuperAdmin)
                {
                    //超级管理员
                    all = db.Queryable<Menu>().Where(z => z.Deleted == 0 && !z.Enable).OrderBy(z => z.OrderCode).ToList();
                    operateList = db.Queryable<T_Sys_MenuOperate, T_Sys_Operate>((M, O) => new object[] { JoinType.Left, M.OperateID == O.ID })
                        .Select((M, O) => new OperateModel
                        {
                            MenuID = M.MenuID,
                            OperateCode = O.Code
                        }).ToList();
                }
                else
                {
                    var roleList = db.Queryable<R_Role_User>().Where(t => t.UserID == this.CurrentUser.UserID).Select(t => t.RoleID).ToList();
                    all = db.Queryable<R_Role_Menu, Menu>((R, M) => new object[] { JoinType.Left, R.MenuID == M.ID }).Where((R, M) => roleList.Contains(R.RoleID) && M.ID > 0 && !M.Enable).Select((R, M) => M).Distinct().ToList();
                    operateList = db.Queryable<R_Role_MenuOperate, T_Sys_MenuOperate, T_Sys_Operate>((R, M, O) => new object[] { JoinType.Left, R.MenuOperateID == M.ID, JoinType.Left, M.OperateID == O.ID }).Where((R, M, O) => roleList.Contains(R.RoleID))
                        .Select((R, M, O) => new OperateModel
                        {
                            MenuID = M.MenuID,
                            OperateCode = O.Code
                        }).ToList();
                }
                all = all.OrderBy(t => t.OrderCode).ToList();
                var typeIDList = all.Select(z => z.MenuType).Distinct().ToList();
                var menutypelist = db.Queryable<Menutype>().Where(t => t.Enable == 0 && typeIDList.Contains(t.ID)).OrderBy(t => t.OrderCode).ToList();

                List<MenuView> ReturnList = new List<MenuView>();
                foreach (var mtype in menutypelist)
                {
                    var childrenView = MenuToView(all.Where(t => t.MenuType == mtype.ID).ToList().BuildTree(0), operateList);
                    var typeView = new MenuView()
                    {
                        id = mtype.ID,
                        path = $"/{mtype.Code}",
                        name = null,// mtype.Code,
                        children = childrenView,
                        meta = new metaclass()
                        {
                            i18n = false,
                            title = mtype.CNName,
                            rank = mtype.OrderCode,
                            icon = mtype.Icon,
                            menuid = mtype.ID
                        }
                    };
                    if (childrenView != null && childrenView.Any())
                    {
                        typeView.redirect = childrenView.FirstOrDefault()!.path;
                    }
                    ReturnList.Add(typeView);
                }
                return ReturnList;
            }
        }

        /// <summary>
        /// 按层级构建树结构
        /// </summary>
        /// <param name="menuList"></param>
        /// <param name="operateList"></param>
        /// <returns></returns>
        private List<MenuView> MenuToView1(List<Menu> menuList, List<OperateModel> operateList)
        {
            //List<MenuView> t=   ExpressionMapper<Menu, MenuView>.Map(menuList);
            List<MenuView> result = new List<MenuView>();
            foreach (var menu in menuList)
            {
                MenuView temp = new MenuView();
                temp.name = menu.CNName;
                temp.id = menu.ID;
                temp.path = menu.MenuPath.Contains("http") ? menu.Remark : menu.MenuPath;
                temp.meta = new metaclass()
                {
                    menuid = menu.ID,
                    i18n = false,
                    icon = menu.Icon,
                    rank = (int)(menu.OrderCode + Math.Pow(100, menu.NodeLevel.Value)),
                    title = menu.CNName,
                    frameSrc = menu.MenuPath.Contains("http") ? menu.MenuPath : null,
                    authority = string.Join(',', operateList.Where(t => t.MenuID == menu.ID).Select(t => t.OperateCode).ToArray())
                };
                if (menu.Children != null && menu.Children.Any())
                {
                    var childrenView = MenuToView1(menu.Children, operateList);
                    if (childrenView != null && childrenView.Any())
                    {
                        temp.redirect = childrenView.FirstOrDefault()!.path;
                        //temp.redirect = null;
                    }
                    else
                    {
                        temp.redirect = null;
                    }
                    //temp.redirect = null;
                    temp.children = childrenView;
                    temp.name = menu.CNName;
                }
                else
                {
                    temp.redirect = null;
                    temp.keepAlive = true;
                    temp.meta.showParent = false;
                    //temp.children = new List<MenuView>();
                }
                result.Add(temp);
            }
            return result;
        }

        /// <summary>
        /// 按层级构建树结构
        /// </summary>
        /// <param name="menuList"></param>
        /// <param name="operateList"></param>
        /// <returns></returns>
        private List<MenuView> MenuToView(List<Menu> menuList, List<OperateModel> operateList)
        {
            //List<MenuView> t=   ExpressionMapper<Menu, MenuView>.Map(menuList);
            List<MenuView> result = new List<MenuView>();
            foreach (var menu in menuList)
            {
                MenuView temp = new MenuView();
                temp.id = menu.ID;
                temp.name = menu.MenuCode.ToLower();
                temp.path = menu.MenuPath.Contains("http") ? menu.Remark : menu.MenuPath;
                temp.meta = new metaclass()
                {
                    menuid = menu.ID,
                    i18n = false,
                    icon = menu.Icon,
                    rank = (int)(menu.OrderCode + Math.Pow(100, menu.NodeLevel.Value)),
                    title = menu.CNName,
                    frameSrc = menu.MenuPath.Contains("http") ? menu.MenuPath : null,
                    authority = string.Join(',', operateList.Where(t => t.MenuID == menu.ID).Select(t => t.OperateCode).ToArray())
                };
                if (menu.Children != null && menu.Children.Any())
                {
                    var childrenView = MenuToView(menu.Children, operateList);
                    if (childrenView != null && childrenView.Any())
                    {
                        temp.redirect = childrenView.FirstOrDefault()!.path;
                        //temp.redirect = null;
                    }
                    else
                    {
                        temp.redirect = null;
                    }
                    //temp.redirect = null;
                    temp.children = childrenView;
                    temp.name = null;
                }
                else
                {
                    temp.redirect = null;
                    temp.keepAlive = true;
                    temp.meta.showParent = false;
                    //temp.children = new List<MenuView>();
                }
                result.Add(temp);
            }
            return result;
        }



        /// <summary>
        /// 转换为路由list
        /// </summary>
        /// <param name="menulist"></param>
        /// <returns></returns>
        public List<typemenu> ConverTolistMenuView(List<Menu> menulist)
        {

            List<typemenu> typemenu = new List<typemenu>();
            var menuTypelist = menulist.GroupBy(z => z.MenuType);
            foreach (var menutypitem in menuTypelist)
            {
                List<MenuView> menuviewlist = new List<MenuView>();
                foreach (var item in menutypitem)
                {
                    var name = "";
                    if (item.MenuPath != null)
                    {
                        var path = item.MenuPath.Split('/');
                        if (path[path.Length - 1] == "index")
                        {
                            //name = path[path.Length - 2];
                        }
                        else
                        {
                            //name = path[path.Length - 1];
                        }
                    }
                    MenuView model = new MenuView
                    {
                        id = item.ID,
                        parentid = item.ParentID,
                        name = name,
                        path = item.MenuPath,
                        meta = new metaclass
                        {
                            keepAlive = true,
                            i18n = false,
                            icon = item.Icon,
                            rank = item.OrderCode,
                            title = item.CNName

                        }
                    };
                    menuviewlist.Add(model);
                }
                typemenu t = new typemenu
                {
                    MenuTypeID = menutypitem.Key,
                    MenuViewS = menuviewlist
                };
                typemenu.Add(t);
            }

            return typemenu;
        }
        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <returns></returns>
        public object GetMenuTree(string keyword, int MenuType)
        {
            using (var db = Db.GetDbContext(String.Empty))
            {
                List<Menu> treelist = new List<Menu>();
                var all = db.Queryable<Menu>().Where(z => z.MenuType == MenuType).WhereIF(!string.IsNullOrEmpty(keyword), z => z.CNName.Contains(keyword) || z.CNName.Contains(keyword)).Where(z => z.Deleted == 0).OrderBy(z => z.OrderCode).ToList();

                foreach (var item in all)
                {
                    var parent = all.Where(z => z.ID == item.ParentID).FirstOrDefault();
                    if (parent != null)
                    {
                        if (parent.Children == null)
                        {
                            parent.Children = new List<Menu>();
                        }
                        parent.Children.Add(item);
                    }
                    else
                    {
                        treelist.Add(item);
                    }


                }
                return treelist;
            }

        }

        /// <summary>
        /// 添加菜单树
        /// </summary>
        /// <returns></returns>
        public object AddMenu(Menu model)
        {
            try
            {
                //获取排序码
                //model.OrderCode = GetOrderCode();
                checkdata(model, 1);
                this.Insert<Menu>(model);
                return true;
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 编辑菜单树
        /// </summary>
        /// <returns></returns>
        public object EditMenu(Menu model)
        {
            try
            {
                checkdata(model, 2);
                this.Update<Menu>(model);
                return true;
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <param name="checktype"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public bool checkdata(Menu model, int checktype)
        {
            using (var db = Db.GetDbContext(""))
            {
                if (checktype == 1) //新增
                {
                    if (db.Queryable<Menu>().Any(z => z.MenuCode == model.MenuCode && z.Deleted != 1))
                    {
                        throw new Exception("编码已经存在");
                    }
                    if (db.Queryable<Menu>().Any(z => z.CNName == model.CNName && z.Deleted != 1))
                    {
                        throw new Exception("名称已经存在");
                    }
                }
                else
                { //编辑
                    if (db.Queryable<Menu>().Any(z => z.MenuCode == model.MenuCode && z.ID != model.ID && z.Deleted != 1))
                    {
                        throw new Exception("编码已经存在");
                    }
                    if (db.Queryable<Menu>().Any(z => z.CNName == model.CNName && z.ID != model.ID && z.Deleted != 1))
                    {
                        throw new Exception("名称已经存在");
                    }
                }
                return true;

            }
        }

        /// <summary>
        /// 删除菜单树
        /// </summary>
        /// <returns></returns>
        public object DeleteMenu(List<int> Ids)
        {
            try
            {
                this.Delete<Menu>(Ids.ToArray());
                return true;
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// </summary>
        /// <param name="source">拖拽源</param>
        /// <param name="target">拖拽目标地</param>
        /// <param name="type">目标地前/后/内</param>
        /// <returns></returns>
        public void DraggleMenu(Menu source, Menu target, int type)
        {

            if (type == 1) //前
            {
                source.ParentID = target.ParentID;
                source.OrderCode = target.OrderCode - 1;
                source.NodeLevel = target.NodeLevel;
                source.PropPath = target.PropPath;
            }
            else if (type == 1) //后
            {
                source.ParentID = target.ParentID;
                source.OrderCode = target.OrderCode + 1;
                source.NodeLevel = target.NodeLevel;
                source.PropPath = target.PropPath;
            }
            else //内
            {
                source.ParentID = target.ID; ;
                source.OrderCode = target.OrderCode - 1;
                source.NodeLevel = target.NodeLevel + 1;
                source.PropPath = source.PropPath + source.ID + "/";
            }
            this.Update<Menu>(source);
        }


        /// <summary>
        /// 获取排序码
        /// </summary>
        /// <returns></returns>
        public int GetOrderCode()
        {
            using (var db = Db.GetDbContext(""))
            {
                var order = db.Queryable<Menu>().Max(z => z.OrderCode);
                if (order == null)
                {
                    return 1;
                }
                else
                {
                    return Convert.ToInt32(order + 1);
                }
            }
        }


        #region 菜单与操作关系
        /// <summary>
        /// 获取已经授权给菜单的操作
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public object GetMenuOperate(int MenuID)
        {

            using (var db = Db.GetDbContext(String.Empty))
            {
                return db.Queryable<Menu, T_Sys_MenuOperate, T_Sys_Operate>((r, t, u) => new object[] {
            JoinType.Right,r.ID==t.MenuID,
            JoinType.Right,t.OperateID==u.ID,
            }).Where((r, t, u) => r.ID == MenuID).Select((r, t, u) => new T_Sys_Operate
            {
                ID = t.ID,
                Code = u.Code,
                CreateTime = u.CreateTime,
                CreatorID = u.CreatorID,
                ModifyID = u.ModifyID,
                ModifyTime = u.ModifyTime,
                Name = u.Name,
                Remark = u.Remark
            }).ToList();
            }
        }


        /// <summary>
        /// 获取还没有授权给菜单的操作
        /// </summary>
        /// <param name="MenuID"></param>
        ///  <param name="keyword"></param>
        /// <returns></returns>
        public object GetNotMenuOperate(int MenuID, string keyword)
        {

            using (var db = Db.GetDbContext(String.Empty))
            {
                var operateids = db.Queryable<Menu, T_Sys_MenuOperate, T_Sys_Operate>((r, t, u) => new object[] {
            JoinType.Right,r.ID==t.MenuID,
            JoinType.Right,t.OperateID==u.ID,
            }).Where((r, t, u) => r.ID == MenuID).Select((r, t, u) => new
            {
                ID = u.ID,

            }).ToList().Select(z => z.ID).ToList();
                return db.Queryable<T_Sys_Operate>().WhereIF(!string.IsNullOrEmpty(keyword), u => u.Name.Contains(keyword) || u.Code.Contains(keyword)).Where(u => !operateids.Contains(u.ID)).Select(u => new
                {
                    ID = u.ID,
                    Code = u.Code,
                    CreateTime = u.CreateTime,
                    CreatorID = u.CreatorID,
                    ModifyID = u.ModifyID,
                    ModifyTime = u.ModifyTime,
                    Name = u.Name,
                    Remark = u.Remark
                }).ToList();
            }
        }

        /// <summary>
        /// 分配操作
        /// </summary>
        /// <param name="MenuID"></param>
        /// <param name="Operateids"></param>
        /// <returns></returns>
        public object AddMenuOperate(int MenuID, List<int> Operateids)
        {
            using (var db = Db.GetDbContext(String.Empty))
            {
                List<T_Sys_MenuOperate> list = new List<T_Sys_MenuOperate>();
                foreach (var operateid in Operateids)
                {
                    T_Sys_MenuOperate t = new T_Sys_MenuOperate
                    {
                        CreateTime = DateTime.Now,
                        CreatorID = CurrentUser.UserID,
                        MenuID = MenuID,
                        ModifyID = CurrentUser.UserID,
                        ModifyTime = DateTime.Now,
                        OperateID = operateid

                    };
                    list.Add(t);
                }
                db.Insertable<T_Sys_MenuOperate>(list).ExecuteCommand();
                return true;
            }
        }
        /// <summary>
        /// 移除操作
        /// </summary>
        /// <param name="tids"></param>
        /// <returns></returns>
        public bool RemoveMenuOperate(List<int> tids)
        {
            try
            {
                return this.Delete<T_Sys_MenuOperate>(tids.ToArray());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        #endregion

        #region 菜单与数据项关系
        /// <summary>
        /// 获取已经授权给菜单的数据项类型
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public object GetMenuDataItem(int MenuID)
        {
            //只需要数据项类型
            using (var db = Db.GetDbContext(String.Empty))
            {
                return db.Queryable<Menu, T_Sys_MenuDataItem, T_Sys_DataItem>((r, t, u) => new object[] {
            JoinType.Right,r.ID==t.MenuID,
            JoinType.Right,t.DataItemID==u.ID,
            }).Where((r, t, u) => r.ID == MenuID).Select((r, t, u) => u.CatatgoryID).Distinct().ToList();
            }
        }
        /// <summary>
        /// 分配操作
        /// </summary>
        /// <param name="MenuID"></param>
        /// <param name="catagoryids"></param>
        /// <returns></returns>
        public object AddMenuDataItem(int MenuID, List<int> catagoryids)
        {
            using (var db = Db.GetDbContext(String.Empty))
            {
                var dataitemids = db.Queryable<T_Sys_DataItem>().Where(z => catagoryids.Contains(z.CatatgoryID) && z.Deleted != 1).Select(z => z.ID).ToList();


                List<T_Sys_MenuDataItem> list = new List<T_Sys_MenuDataItem>();
                foreach (var dataitemid in dataitemids)
                {
                    T_Sys_MenuDataItem t = new T_Sys_MenuDataItem
                    {
                        CreateTime = DateTime.Now,
                        CreatorID = CurrentUser.UserID,
                        MenuID = MenuID,
                        ModifyID = CurrentUser.UserID,
                        ModifyTime = DateTime.Now,
                        DataItemID = dataitemid

                    };
                    list.Add(t);
                }
                db.Insertable<T_Sys_MenuDataItem>(list).ExecuteCommand();
                return true;
            }
        }

        /// <summary>
        /// 移除操作
        /// </summary>
        /// <param name="tids"></param>
        /// <returns></returns>
        public bool RemoveDataItem(int MenuId, List<int> Catagoryids)
        {
            try
            {

                using (var db = Db.GetDbContext(String.Empty))
                {
                    var Ids = db.Queryable<T_Sys_DataItem>().Where(z => Catagoryids.Contains(z.CatatgoryID)).Select(z => z.ID).ToList();
                    var tids = db.Queryable<T_Sys_MenuDataItem>().Where(z => z.MenuID == MenuId && Ids.Contains(z.DataItemID)).Select(z => z.ID).ToList();
                    return this.Delete<T_Sys_MenuDataItem>(tids.ToArray());
                }

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        public object GetMenuNotUsual()
        {
            using (var db = Db.GetDbContext(String.Empty))
            {

                List<Menu> all = new List<Menu>();
                List<OperateModel> operateList;
                if (CurrentUser!.UserType == (int)EUserType.SuperAdmin)
                {
                    //超级管理员
                    all = db.Queryable<Menu, T_Sys_UsualFunction>((sa, sb) => new object[] { JoinType.Left, sa.ID == sb.MenuID }).Where((sa, sb) => sa.Deleted == 0).OrderBy((sa, sb) => sa.OrderCode).ToList();
                    operateList = db.Queryable<T_Sys_MenuOperate, T_Sys_Operate>((M, O) => new object[] { JoinType.Left, M.OperateID == O.ID })
                        .Select((M, O) => new OperateModel
                        {
                            MenuID = M.MenuID,
                            OperateCode = O.Code
                        }).ToList();
                }
                else
                {
                    var roleList = db.Queryable<R_Role_User>().Where(t => t.UserID == this.CurrentUser.UserID).Select(t => t.RoleID).ToList();
                    all = db.Queryable<R_Role_Menu, Menu, T_Sys_UsualFunction>((R, M, U) => new object[] { JoinType.Left, R.MenuID == M.ID, JoinType.Left, M.ID == U.MenuID }).Where((R, M, U) => roleList.Contains(R.RoleID) && M.ID > 0).Select((R, M, U) => M).Distinct().ToList();
                    operateList = db.Queryable<R_Role_MenuOperate, T_Sys_MenuOperate, T_Sys_Operate>((R, M, O) => new object[] { JoinType.Left, R.MenuOperateID == M.ID, JoinType.Left, M.OperateID == O.ID }).Where((R, M, O) => roleList.Contains(R.RoleID))
                        .Select((R, M, O) => new OperateModel
                        {
                            MenuID = M.MenuID,
                            OperateCode = O.Code
                        }).ToList();
                }
                var typeIDList = all.Select(z => z.MenuType).Distinct().ToList();
                var menutypelist = db.Queryable<Menutype>().Where(t => t.Enable == 0 && typeIDList.Contains(t.ID)).OrderBy(t => t.OrderCode).ToList();

                List<MenuView> ReturnList = new List<MenuView>();
                foreach (var mtype in menutypelist)
                {
                    var childrenView = MenuToView1(all.Where(t => t.MenuType == mtype.ID).ToList().BuildTree(0), operateList);
                    var typeView = new MenuView()
                    {
                        id = 0 - mtype.ID,
                        path = $"/{mtype.Code}",
                        name = mtype.CNName,// mtype.Code,
                        children = childrenView,
                        meta = new metaclass()
                        {
                            i18n = false,
                            title = mtype.CNName,
                            rank = mtype.OrderCode,
                            icon = mtype.Icon
                        }
                    };
                    if (childrenView != null && childrenView.Any())
                    {
                        typeView.redirect = childrenView.FirstOrDefault()!.path;
                    }
                    ReturnList.Add(typeView);
                }
                return ReturnList;

            }
        }

        public object GetUsualMenu()
        {
            using (var db = Db.GetDbContext(String.Empty))
            {

                List<Menu> all = new List<Menu>();
                List<OperateModel> operateList;
                if (CurrentUser!.UserType == (int)EUserType.SuperAdmin)
                {
                    //超级管理员
                    all = db.Queryable<Menu, T_Sys_UsualFunction>((sa, sb) => new object[] { JoinType.Left, sa.ID == sb.MenuID }).Where((sa, sb) => sa.Deleted == 0 && sb.ID != null).OrderBy((sa, sb) => sa.OrderCode).ToList();
                    operateList = db.Queryable<T_Sys_MenuOperate, T_Sys_Operate>((M, O) => new object[] { JoinType.Left, M.OperateID == O.ID })
                        .Select((M, O) => new OperateModel
                        {
                            MenuID = M.MenuID,
                            OperateCode = O.Code
                        }).ToList();
                }
                else
                {
                    var roleList = db.Queryable<R_Role_User>().Where(t => t.UserID == this.CurrentUser.UserID).Select(t => t.RoleID).ToList();
                    all = db.Queryable<R_Role_Menu, Menu, T_Sys_UsualFunction>((R, M, U) => new object[] { JoinType.Left, R.MenuID == M.ID, JoinType.Left, M.ID == U.MenuID }).Where((R, M, U) => roleList.Contains(R.RoleID) && M.ID > 0 && U.ID != null).Select((R, M, U) => M).Distinct().ToList();
                    operateList = db.Queryable<R_Role_MenuOperate, T_Sys_MenuOperate, T_Sys_Operate>((R, M, O) => new object[] { JoinType.Left, R.MenuOperateID == M.ID, JoinType.Left, M.OperateID == O.ID }).Where((R, M, O) => roleList.Contains(R.RoleID))
                        .Select((R, M, O) => new OperateModel
                        {
                            MenuID = M.MenuID,
                            OperateCode = O.Code
                        }).ToList();
                }

                List<MenuView> ReturnList = new List<MenuView>();

                ReturnList = MenuToView1(all.ToList(), operateList);

                return ReturnList;

            }
        }

        public object AddOUsualFunction(List<int> ids)
        {
            using (var db = Db.GetDbContext(String.Empty))
            {
                List<T_Sys_UsualFunction> usuallist = new List<T_Sys_UsualFunction>();
                db.Deleteable<T_Sys_UsualFunction>().ExecuteCommand();
                foreach (var id in ids)
                {
                    T_Sys_UsualFunction entity = new T_Sys_UsualFunction
                    {
                        CreatorID = CurrentUser.UserID,
                        MenuID = id,
                        CreateTime = DateTime.Now,
                        ModifyTime = DateTime.Now,
                        Sort = 1,
                        ModifyID = CurrentUser.UserID
                    };
                    usuallist.Add(entity);
                }
                return db.Insertable<T_Sys_UsualFunction>(usuallist).ExecuteCommand();
            }
        }
        #endregion



    }
}
